package com.bookstore.service;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.bookstore.dao.AccountDAO;
import com.bookstore.dao.BookDAO;
import com.bookstore.dao.TradeDAO;
import com.bookstore.dao.TradeItemDAO;
import com.bookstore.dao.UserDAO;
import com.bookstore.dao.impl.AccountDAOImpl;
import com.bookstore.dao.impl.BookDAOImpl;
import com.bookstore.dao.impl.TradeDAOImpl;
import com.bookstore.dao.impl.TradeItemDAOImpl;
import com.bookstore.dao.impl.UserDAOImpl;
import com.bookstore.db.JDBCUtils;
import com.bookstore.domain.Book;
import com.bookstore.domain.ShoppingCart;
import com.bookstore.domain.ShoppingCartItem;
import com.bookstore.domain.Trade;
import com.bookstore.domain.TradeItem;
import com.bookstore.web.CriteriaBook;
import com.bookstore.web.Page;

public class BookService {
    
    private BookDAO bookDAO = new BookDAOImpl();
    public Page<Book> getPage(CriteriaBook criteriaBook) {
        return bookDAO.getPage(criteriaBook);
    }
    
    public Book getBook(int id) {
		return bookDAO.getBook(id);
	}

	public boolean addToCart(int id, ShoppingCart sCart) {
		Book book = bookDAO.getBook(id);
		if (book != null) {
			sCart.addBook(book);
			return true;
		}
		return false;
	}

	public void removeItemFromShoppingCart(ShoppingCart sCart, int id) {
		sCart.removeItem(id);
	}

	public void clearShoppingCart(ShoppingCart sCart) {
		sCart.clear();
	}

	public void updateItemQuantity(ShoppingCart sCart, int id, int quantity) {
		sCart.updateItemQuantity(id, quantity);
	}
	
	private AccountDAO accountDAO = new AccountDAOImpl();
	private TradeDAO tradeDAO = new TradeDAOImpl();
	private UserDAO userDAO = new UserDAOImpl();
	private TradeItemDAO tradeItemDAO = new TradeItemDAOImpl();
	
	// 业务方法
	public void checkout(ShoppingCart shoppingCart, String username, String accountId) {
		
		/*
		// 普通的事务控制方式
		Connection connection = null;
		try {
			connection = JDBCUtils.getConnection();
			// 关闭自动提交,开启事务
			connection.setAutoCommit(false);
			
			DAO操作需要传入connection
			...
			...
			...
			
			//提交、关闭事务
			connection.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		} finally {
			JDBCUtils.release(connection);
		}
		*/
		
		// 1. 更新mybooks 数据表相关记录的saleamount 和 storenumber
		bookDAO.batchUpdateStoreNumberAndSalesAmount(shoppingCart.getItems());
		
		// 2. 更新余额 account 表的 balance
		accountDAO.updateBalance(Integer.parseInt(accountId), shoppingCart.getTotalMoney());
		
		// 3. 向 trade 数据表插入记录
		Trade trade = new Trade();
		trade.setUserId(userDAO.getUser(username).getUserid());
		trade.setTradeTime(new Date(new java.util.Date().getTime()));
		tradeDAO.insert(trade);
		
		// 4. 向 tradeitem 表插入商品记录
		Collection<TradeItem> items = new ArrayList<>();
		for (ShoppingCartItem sCartItem : shoppingCart.getItems()) {
			TradeItem tradeItem = new TradeItem();
			tradeItem.setBookId(sCartItem.getBook().getId());
			tradeItem.setQuantity(sCartItem.getQuantity());
			tradeItem.setTradeId(trade.getTradeId());
			items.add(tradeItem);
		}
		tradeItemDAO.batchSave(items);
		
		// 5. 清空购物车
		shoppingCart.clear();
		
	}
}
